package dth.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import dth.entity.SetupBox;

public class SetupBoxDAO {
	private SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	private Session session=sessionFactory.openSession();
	public java.util.List<SetupBox> getListSetupBox()
	{
		session.beginTransaction();
		String queryString="select * from setup_box";
		Query query=session.createSQLQuery(queryString).addEntity(SetupBox.class);
		return query.list();
	}
	public int delete(int id)
	{
		Transaction tx=session.beginTransaction();
		String queryString="delete from setup_box where setupbox_id= :id";
		Query query=session.createSQLQuery(queryString).setParameter("id", id);
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
	public int insert(SetupBox setup)
	{
		
		Transaction tx=session.beginTransaction();
		if(setup.getType().trim()=="" && setup.getPrice() <= 0 ) 
		{
			return 4;
		}
		if(setup.getType().trim()=="")
		{
			return 3;
		}
		if(checkType(setup.getType().trim()) )
		{
			return 5;
		}
		if(setup.getPrice() <= 0)
		{
			return 2;
		}
		if(setup.getImage().trim().equals(""))
		{
			return 6;
		}
		
		
		String queryString="INSERT into setup_box(setupbox_type,setupbox_price,fk_admin_id,date_insert,setupbox_image,setupbox_description) VALUES(:type,:price,:fkAdminId,:date_insert,:image,:des)";
		
		Query query=session.createSQLQuery(queryString);
		query.setParameter("type",setup.getType());
		query.setParameter("price",setup.getPrice());
		query.setParameter("fkAdminId",setup.getFkAdminId());
		query.setParameter("date_insert",setup.getDateInsert());
		query.setParameter("image",setup.getImage());
		query.setParameter("des",setup.getDescription());
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
	public Boolean checkType(String type) {
		
		
		String queryString="select count(*) from setup_box where setupbox_type=:type";
		Query query=session.createSQLQuery(queryString);
		query.setParameter("type",type);
		
		if(query.list().get(0).toString() == "0")
			return false;
		return true;
	}
	public SetupBox getSetupBox(int id)
	{
		
		String queryString="select * from setup_box where setupbox_id=:id";
		Query query=session.createSQLQuery(queryString);
		query.setParameter("id",id);
		SetupBox st =  (SetupBox) query.list().get(0);
		return st;
	}
	public int update(SetupBox setup)
	{
		
		if(setup.getType().trim()=="" && setup.getPrice() <= 0 )
		{
			return 4;
		}
		if(setup.getType().trim()=="")
		{
			return 3;
		}
		if(setup.getPrice() <= 0)
		{
			return 2;
		}
		
		
		Transaction tx=session.beginTransaction();
		String queryString="update setup_box set setupbox_description=:des,setupbox_image=:image,setupbox_type=:type,setupbox_price=:price,fk_admin_id=:fkAdminId,date_insert=:date where setupbox_id=:id";
		Query query=session.createSQLQuery(queryString);
		query.setParameter("id",setup.getId());
		query.setParameter("type",setup.getType());
		query.setParameter("price",setup.getPrice());
		query.setParameter("fkAdminId",setup.getFkAdminId());
		query.setParameter("date",setup.getDateInsert());
		query.setParameter("image",setup.getImage());
		query.setParameter("des",setup.getDescription());
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
}
